package com.yuwj.chinaa.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

//账户修改昵称界面
public class AccountUpdateNicknameActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    IPresenter presenter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_account_update_nickname;
    }

    public static Intent createIntent(Context context) {
        return new Intent(context, AccountUpdateNicknameActivity.class);
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);

    }

    @Override
    public void initView() {
        tvTitle.setText("设置昵称");
    }

    @Override
    public void initOperator() {

    }

    @Override
    public void onUserUpdateNameSuccess() {
        super.onUserUpdateNameSuccess();
        ToastUtil.getInstance().shortLength(context, "修改成功");
        String name = etNickname.getText().toString().trim();
        entityUser.setName(name);
        SharedpreferenceUtil.getInstance().saveUserInfo(context, entityUser);
        finish();
    }

    @Override
    public void onUserUpdateNameFail(Exception e) {
        super.onUserUpdateNameFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }


    @OnClick({R.id.iv_back, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_confirm:
                if (TextUtils.isEmpty(etNickname.getText().toString().trim())) {
                    ToastUtil.getInstance().shortLength(context, "请输入昵称");
                    return;
                }
                //todo 修改昵称
                String name = etNickname.getText().toString().trim();
                JSONObject param = new JSONObject();
                try {
                    param.put("nicknname", name);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.userUpdateName(param.toString());

                break;
        }
    }
}
